/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*   File:      AGV_Message_Sendand_Receive.cpp
*   Author:    刘洋兵
*   Function:  发送包装的json协议数据/解析
*   Time:      2025-09-09
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "CommandSendorReceive.h"

QMutex CommandSendorReceive::m_mutex;

/**
 * @brief  构造函数
 * @param  parent 父窗口
 */
CommandSendorReceive::CommandSendorReceive(QObject *parent) : QObject(parent)
{

}

/**
 * @brief  析构函数
 */
CommandSendorReceive::~CommandSendorReceive()
{

}

/**
 * @brief 获取AGV_Message_Sendand_Receive单例实例
 * @return AGV_Message_Sendand_Receive实例应用
 */
CommandSendorReceive& CommandSendorReceive::getInstance()
{
    static CommandSendorReceive m_instance;
    return m_instance;
}

/**
 * @brief  数据下发到中间通信层
 * @param  dataMap 封装数据库操作数据时需包含'data'嵌套的json结构
 * @param  infomap 封装的基本协议数据
 * @param  function_code 功能号
 * @param  is_data 是否包含数据库操作函数
 */
void CommandSendorReceive::sendCommand(const QMap<QString, QVariant>& dataMap,
                                              const QMap<QString, QVariant>& infomap,
                                              const int& function_code,
                                              bool is_data)
{
    PackageData converter;

    // 设置request_id
    QUuid uuid = QUuid::createUuid();
    QString request_id = uuid.toString(QUuid::WithoutBraces);
    // 检测重复
    while(isRepeatId(request_id))
    {
        uuid = QUuid::createUuid();
        request_id = uuid.toString(QUuid::WithoutBraces);
    }

    // 添加request_id监听容器
    SaveMessage saveM;
    saveM.function_code = function_code;
    if(infomap.contains("table_name"))
    {
        saveM.tablename = infomap["table_name"].toString();
    }
    m_request_ids[request_id] = saveM;


    // 打包成json数据结构
    QByteArray jsonData = converter.convertToJson(dataMap,
                                                  infomap,
                                                  function_code,
                                                  request_id,
                                                  is_data);
    qDebug() << "Generated JSON:\n" << jsonData;
    //MQClient::getInstance()->publishMessageAsync(jsonData);   //mq发送json数据
    //MQClient::getInstance()->publishMessage(jsonData);
}

/**
 * @brief  数据解析
 */
void CommandSendorReceive::receiveCommand()
{

}

/**
 * @brief  检测request_id是否重复
 * @param  id 检测对象
 * @return 返回是否重复
 */
bool CommandSendorReceive::isRepeatId(const QString& id)
{
    QMutexLocker locker(&m_mutex);
    for (auto it = m_request_ids.begin(); it != m_request_ids.end(); ++it) {
        if (it.key() == id) {
            return true;
        }
    }
    return false;
}

/**
 * @brief  删除m_request_ids容器中某个对象
 * @param  id 删除对象
 */
void CommandSendorReceive::deleteRequestId(const QString& id)
{
    QMutexLocker locker(&m_mutex);
    if(m_request_ids.contains(id))
    {
        m_request_ids.remove(id);
    }
}

/**
 * @brief  返回request_id指向的结构体对象
 * @param  id 检测对象
 * @return 返回SaveMessage结构体对象
 */
SaveMessage CommandSendorReceive::getSaveMessage(const QString& id)
{
    SaveMessage S_M;
    if(m_request_ids.contains(id))
    {
        return m_request_ids[id];
    }
    return S_M;
}

/**
 * @brief  返回QMessageBox窗口点击结果
 * @param  Title 提示窗口标题
 * @param  Text 提示窗口内容
 * @return 返回是否操作
 */
bool CommandSendorReceive::isExecute(const QString& Title, const QString& Text)
{
    QMessageBox msgBox;
    msgBox.setWindowTitle(Title);
    msgBox.setText(Text);
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setButtonText(QMessageBox::Ok, "确认操作");  // 修改OK按钮文本
    msgBox.setButtonText(QMessageBox::Cancel, "取消操作"); // 修改Cancel按钮文本
    msgBox.setStyleSheet("QLabel{font-size:16px; padding:10px;}");

    msgBox.setStyleSheet("QDialogButtonBox {"
                         "alignment: center;"
                         "}"
                         "QLabel{"
                         "padding:10px;"
                         "font-size: 16px;"  // 控制字体大小
                         "}");
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok) {
        return true;
    }else {
        return false;
    }
}
